package com.yicao.yicaomall.product.controller;

import java.util.Arrays;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.yicao.yicaomall.product.entity.BrandEntity;
import com.yicao.yicaomall.product.service.BrandService;
import com.yicao.yicaomall.common.utils.PageUtils;
import com.yicao.yicaomall.common.utils.R;


/**
 * 品牌
 *
 * @author yicao
 * @email yicaozyq@gmail.com
 * @date 2023-10-31 16:57:55
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @Value("${qiniuyun.domain}")
    private String domain;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        // 调用品牌服务的queryPage方法查询分页数据
        PageUtils page = brandService.queryPage(params);

        // 返回成功，并将分页数据放入返回结果的"page"字段中
        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);
        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody BrandEntity brand) {
        if (brand.getLogo()!= null && !brand.getLogo().startsWith("http")) {
            brand.setLogo(domain + brand.getLogo());
        }
        brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody BrandEntity brand) {
        System.out.println(brand.getLogo());
        if (brand.getLogo()!= null && !brand.getLogo().startsWith("http")) {
            brand.setLogo(domain + brand.getLogo());
        }
        brandService.saveAndUpdateRelation(brand);

        return R.ok();
    }

    /**
     * 删除品牌
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }


    /**
     * 修改状态
     */
    @RequestMapping("/update/status")
    public R updateStatus(@RequestBody BrandEntity brand) {
        brandService.updateById(brand);
        return R.ok();
    }

}
